export function selectFileType(type) {
  const word = [
    ".djvu",
    ".doc",
    ".docm",
    ".docx",
    ".docxf",
    ".dot",
    ".dotm",
    ".dotx",
    ".epub",
    ".fb2",
    ".fodt",
    ".htm",
    ".html",
    ".mht",
    " .mhtml",
    ".odt",
    ".oform",
    ".ott",
    ".oxps",
    ".pdf",
    ".rtf",
    ".stw",
    ".sxw",
    ".txt",
    ".wps",
    ".wpt",
    ".xml",
    ".xps"
  ]

  const cell = [
    ".csv",
    ".et",
    ".ett",
    ".fods",
    ".ods",
    ".ots",
    ".sxc",
    ".xls",
    ".xlsb",
    ".xlsm",
    ".xlsx",
    ".xlt",
    " .xltm",
    ".xltx",
    ".xml"
  ]

  const slide = [
    ".dps",
    ".dpt",
    ".fodp",
    ".odp",
    ".otp",
    ".pot",
    ".potm",
    ".potx",
    ".pps",
    " .ppsm",
    ".ppsx",
    ".ppt",
    ".pptm",
    ".pptx",
    ".sxi"
  ]
  if (word.includes(type)) {
    return "word"
  } else if (cell.includes(type)) {
    return "cell"
  } else if (slide.includes(type)) {
    return "slide"
  } else {
    return ""
  }
}

// @param  {String} url 目标文件地址
// @param  {String} filename 想要保存的文件名称
export function courseDownload(url, filename, onprogress = () => {}) {
  getBlob(
    url,
    function (blob) {
      saveAs(blob, filename)
    },
    onprogress
  )
}
function getBlob(url, cb, onprogress) {
  let xhr = new XMLHttpRequest()
  xhr.open("GET", url, true)
  xhr.responseType = "blob"
  xhr.onload = function () {
    if (xhr.status === 200) {
      cb(xhr.response)
    }
  }
  xhr.onloadend = function () {}
  xhr.onprogress = onprogress
  xhr.send()
}
// 保存
// @param  {Blob} blob
// @param  {String} filename 想要保存的文件名称
function saveAs(blob, filename) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename)
  } else {
    let link = document.createElement("a")
    link.href = window.URL.createObjectURL(blob)
    link.download = filename
    link.click()
    window.URL.revokeObjectURL(link.href)
  }
}
